<%
 /****
  * OIVM_Template.jsp
  * This page is a template for the content of the Optimized Interactive View Mode (OIVM).
  *
  * Copyright 2009 MicroStrategy Incorporated. All rights reserved.
  * version: 1.0
  * xhtml: true
  ****/
%><%@ page errorPage="JSP_Error.jsp"
%><%@ page contentType="text/html; charset=UTF-8"
%><%@ page import="com.microstrategy.web.app.utils.JavaScriptBundles"
%><%@ taglib uri="/webUtilTL.tld" prefix="web" %>

<%-- Start up the Web Statistics... --%>
<web:performanceTimer action="initialize" />
<web:performanceTimer action="start" />

<%!
    // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    // C o n s t a n t s . . .
    // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    
    // This is the URL Parameter that determines we are in "development" mode
    // or not. It is used to define how JavaScript resources are included.
    String URL_PARAM_DEVELOPMENT_MODE = "devmode";
    
    // The name of our Mojo bundle.
    String MOJO_BUNDLE_NAME = "mojo-oivm";
    
    // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    // M e t h o d s . . .
    // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    
    // This method simply generates a reference to a JavaScript file
    void includeJavaScriptFile(JspWriter out, String path) throws java.io.IOException {
    	out.println("<script src=\"" + path + "\"></script>");
    }
    
    // This method includes all of the individual (source) JavaScript files in a bundle
    void includeSourceBundleFiles(JspWriter out, String bundleName, String resPrefix) throws java.io.IOException {
    	// Get the Bundle object...
	    JavaScriptBundles.JavaScriptBundle bundle = JavaScriptBundles.getInstance().get(bundleName);
    	
    	// Check for missing bundle...
    	if (bundle == null) {
    		out.println("<!-- Unable to load bundle '" + bundleName + "' -->");
    		return;
    	}

    	// Loop through the files in the bundle...
    	for (int i=0, s = bundle.getFiles().size(); i<s; i++) {
    		// Get the bundle...
    		JavaScriptBundles.JavaScriptFile f = (JavaScriptBundles.JavaScriptFile) bundle.getFiles().get(i);
    		
    		// Generate the inclusion of the file... 
    		includeJavaScriptFile(out, resPrefix + f.getPath() + "/" + f.getFilename());
    	}
    }
    
    // This method includes just the named bundle...
    void includeBundleFile(JspWriter out, String bundleName, String resPrefix) throws java.io.IOException {
    	try {
	    	// Get the relative path to the named bundle...
	    	String bundlePath = JavaScriptBundles.getInstance().getBundlePath(bundleName);
	    	
	    	// Construct the script to include the file...
	    	includeJavaScriptFile(out, resPrefix + bundlePath);
    	}
    	catch (IllegalArgumentException e) {
    		// Error message!
    		out.println("<!-- Unable to resolve path to bundle '" + bundleName + "' -->");
    	}
    }
%>
<%
    // Whether or not we are in "development mode".
    boolean devMode = (request.getParameter(URL_PARAM_DEVELOPMENT_MODE) != null && 
    				   Boolean.valueOf(request.getParameter(URL_PARAM_DEVELOPMENT_MODE)).booleanValue()) || 
    				   false; /* Change this 'false' to 'true' to have it on by default! */
    
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " >

<html>
<head>
<web:metaContentType/>

<!--  favicon -->
<link rel="shortcut icon" href="../style/mstr/images/favicon.ico" type="image/x-icon" />
<!--  end favicon -->      

<title><web:beanValue property="title" encode="true"/>. MicroStrategy 9</title>

<%-- Load any system-wide CSS files... --%>
<link rel="stylesheet" type="text/css" href="../javascript/mojo/css/core.css" />

<web:ifFeature name="doubleByte"><web:then>
<web:resource type="style" name="mstr/mstrDB.css"/>
</web:then></web:ifFeature>

<%--  
  This custom tag automatically includes the "global.css" and "{PAGENAME}Page.css" files found insite the "/style" subfolder of a 
  plugin folder.
  It can be used to customize the look and feel of the application without modifying any configuration file or jsp.
--%>
<web:resource type="custom-style" />

</head>
<web:ifErrorValue>
	<web:then>
                <%-- If not prompted, only show the alert box if the RWBean is not going to render the error message itself: --%>
                <web:ifBeanValue bean="rwb" property="getXMLStatus" value="4">
    <web:then>
        <body class=" <web:ifConnectionValue><web:then><web:value type='preference' name='colorTheme' /></web:then><web:else><web:value type='browserSetting' name='colorTheme' /></web:else></web:ifConnectionValue>">
            <%-- This is where we place the generated content. --%>
            <span id="pagePlaceholder"></span>
        </body>
    </web:then>
                    <web:else>
                		<%@include file='/jsp/Error_FullContent.jsp' %>
                	</web:else>
                </web:ifBeanValue>
	</web:then>
	<web:else>
		<body class=" <web:ifConnectionValue><web:then><web:value type='preference' name='colorTheme' /></web:then><web:else><web:value type='browserSetting' name='colorTheme' /></web:else></web:ifConnectionValue>">
			<%-- This is where we place the generated content. --%>
			<span id="pagePlaceholder"></span>
		</body>
	</web:else>
</web:ifErrorValue>

<script type="text/javascript">
//Set application globals.
var mstrApp = {
    Privs : '<web:connectionValue property="privsXML"/>',
    FlashResBundleURL : '<web:value name="resBundles/DashboardViewerBundle_" type="flashResURL"/>',    
    name: '<web:value type="config" name="servletDeploymentName"/>',
    pageName: '<web:beanValue property="name"/>',
    //_JSP[    
    <%-- no need to add jsession id for asp --%>
    <web:ifFeature type="systemPreference" name="useCookies" value="1"><web:then><%-- add jsessionid only cookie is not disabled --%>
	 sessionId: '<web:connectionValue property="containerSessionId" />',
    </web:then></web:ifFeature>
	 //_JSP]  
    sessionState:'<web:connectionValue property="sessionState"/>',
    servletState: '<web:connectionValue property="servletState"/>',
    localeId: '<web:connectionValue property="locale"/>',
    displayLocaleId: '<web:connectionValue property="displayLocaleID"/>',
    thousandsSep: '<web:value type="misc" name="thousandSeparator"/>',
    decimalSep: '<web:value type="misc" name="decimalSeparator"/>',
    listSep: '<web:value type="misc" name="listSeparator"/>',
    taskURL: '<web:taskProcessorName />',
    jsRoot: '../javascript/',   <%-- TODO: Need to send down actual value --%>
    jsMojoRoot: '../javascript/mojo/js/source/',
    mstrDescs : <web:bundleDescriptor name="mojo-oivm"/>,
    persistTaskParams: <web:value type="persistParameters" name=""/>,
    getPersistParams: function () {return this.persistTaskParams;},
    prevViewMedia: <web:beanValue property="previousViewMedia" encode="true"/>,
    isOIVM: function() {return true;}
};

</script>
<%-- Include the Mojo JavaScript bundle! --%>
<%	if (devMode) {
		// Include all of the individual JavaScript files...
		includeSourceBundleFiles(out, MOJO_BUNDLE_NAME, "");
	} else {
		// Include just the Bundle!
		includeBundleFile(out, MOJO_BUNDLE_NAME, "../");
	}
%>
<%//_ASPTag[
/*
<web:resource type="javascript" name="bundles/mojo-oivm.js" />
*/
//_ASPTag]%>

<script type="text/javascript">
//Create a local namespace.
var local = {};

local.doc = new mstrmojo.Doc({
	slot: 'stack',
	builder: mstrmojo.DocBuilder
});

local.tb = new mstrmojo.ToolBar({
	slot: 'toolbar',
    cssClass: 'mstrmojo-oivmSprite grouped',
	builder: mstrmojo.ToolBarBuilder
});

// Create page widget.
local.view = new mstrmojo.OIVMPage({
	placeholder: 'pagePlaceholder',
    <web:ifBeanValue name="rwb" property="getXMLStatus" value="4"> <!-- WebBeanRequestEndsInError -->
    <web:then>
    error:  <web:displayBean beanName="rwb" />,  
    </web:then>
    </web:ifBeanValue>
	children: [local.tb, 
	    new mstrmojo.TabContainer({
			slot: 'layout',
			layoutConfig: {
				h: {
					top: 'auto',
					stack: '100%'
				},
				w: {
					top: '100%',
					stack: '100%'
				}
			},
			children: [ 
				new mstrmojo.ScrollingTabStrip({
					slot: 'top',
					cssClass: 'mstrmojo-layout-tabs',
					visible: false, // Should only be shown when tabs are added.
					bindings: {
						width: "this.parent.width"
					}
					
				}), 
				local.doc
			]
		})
	]
});

//Declare and render a data-less view first, before parsing thru data JSON.
//This allows HTML to be shown as early as possible, and lets it resize itself
//to fit to window before it gets populated with lots of content.
//This is good for perceived performance.
local.view.render();

// only ouput data when bean is not in error state
<web:ifBeanValue name="rwb" property="getXMLStatus" value="4"> <!-- WebBeanRequestEndsInError -->
<web:else>

//Create and set the toolbar model.
local.tb.model = new mstrmojo.ToolBarModel(<web:displayBean beanName="ribbonBean" />);

// Set the toolbar's target to the document.
local.tb.target = local.doc;

//Now add the actual data.
local.doc.model = new mstrmojo.DocModel(<web:displayBean beanName="rwb" />);

// session expire handling
mstrApp.docModel = local.doc.model;
mstrApp.onSessionExpired = function() {
	var app = mstrmojo.App, 
	   m = app.docModel;
    mstrmojo.requiresCls("mstrmojo.form");
    mstrmojo.form.send({
        evt: 5005,            // PAGE_REFRESH
        src: app.name + "." + app.pageName + ".5005",
//        messageID: m.mid
//        documentID: m.oid
        rwb: m.bs
    });
};

// Temp workaround: copy the features list from the toolbar model to its target's model.
mstrmojo.hash.copy(local.tb.model.features, local.tb.target.model.features);

// Now that the data model is ready, build toolbar children and doc children.
local.tb.buildChildren();
local.doc.buildChildren();

// Kill the reference for GC.
local = null;
</web:else>
</web:ifBeanValue>
</script>

<%-- Trigger the collection of Web Statistics --%>
<web:logging action="statistics" />

</html>